import 'package:dio/dio.dart';
import 'package:base_plugin/base_plugin.dart';

class HttpCommonService {
  /// 获取地址
  static Future<Response> searchAddress(String search) {
    return BaseConfig.httpBase.get('/api/address', {'search': search});
  }

  /// 获取订单号码
  /// [type]  bill/invoice/purchase_order/quotation/credit_note/expense/product/service/payslip/journal_entry
  static Future<Response> suggestNumber(String type, int parentAccountId) {
    return BaseConfig.httpBase.get('/api/common/number_suggest',
        {'type': type, 'parent_account_id': parentAccountId});
  }

  /// 判断号码是否存在
  static Future<Response> numberExist(String type, String number) {
    return BaseConfig.httpBase
        .get('/api/common/number_exist', {'type': type, 'number': number});
  }

  static String downloadPDFUrl(String type, int typeId) {
    if (type == 'subscription') {
      return '${BaseConfig.httpBase.baseUrl}/api/subscriptions/$typeId?is_pdf=1';
    } else if (type == 'soa') {
      return '${BaseConfig.httpBase.baseUrl}/api/statementofaccount/$typeId?is_pdf=1';
    } else {
      return '${BaseConfig.httpBase.baseUrl}/api/pdfs?type=$type&type_id=$typeId';
    }
  }

  /// 下载pdf
  static Future<Response> downloadPDF(String filePath, String type, int typeId,
      [CancelToken cancelToken, ProgressCallback onReceiveProgress]) {
    if (type == 'subscription') {
      return BaseConfig.httpBase.download('/api/subscriptions/$typeId?is_pdf=1',
          filePath, {}, cancelToken, onReceiveProgress);
    } else {
      return BaseConfig.httpBase.download(
          '/api/pdfs',
          filePath,
          {
            'type': type,
            'type_id': typeId,
          },
          cancelToken,
          onReceiveProgress);
    }
  }

  /// 获取 tag
  static Future<Response> getTags(String type) async {
    return BaseConfig.httpBase.get('/api/tags', {
      'type': type,
    });
  }

  /// 获取 tag
  static Future<Response> currentRate(
      String fromCurrency, String defaultCurrency, String date) async {
    return BaseConfig.httpBase.get('/api/common/currency_rate', {
      'from_currency': fromCurrency,
      'to_currency': defaultCurrency,
      'date': date,
    });
  }
}
